package com.nace.book;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;

public class BookBuilder {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		String bookDir = "C:\\Documents and Settings\\sapunce\\Desktop\\Ajzyk Azimov\\Ajzyk Azimov - Pozitronni roboti";
		String bookName = "2. Goloto slyntse-Ajzyk Azimov.txt";
		String chapterMarker = ">";
		createMobileBook(bookDir, bookName, chapterMarker);
	}

	private static void createMobileBook(String bookDir, String bookName, String chapterMarker) {
		String newDirName= bookDir + File.separatorChar + bookName.substring(0, bookName.length() - 4);
		File newDir = new File(newDirName);
		newDir.mkdir();
		String book = bookDir + File.separatorChar + bookName;
		BufferedReader in = null;
		try {
	        in = new BufferedReader(new InputStreamReader(new FileInputStream(book), "UTF8"));
	        StringBuilder sb = new StringBuilder();
	        String str;
	        int prefix = 0;
	        while ((str = in.readLine()) != null) {
	        	System.out.println(str);
	        	if (str.startsWith(chapterMarker)) {
	        		prefix++;
	        		String prefixStr = String.valueOf(prefix);
	        		if (prefixStr.length() == 1) {
	        			prefixStr = "0" + prefixStr;
	        		}
	        		createBookPart(newDirName, bookName, sb.toString(), prefixStr);
	        		sb.delete(0, sb.length() - 1);
	        		if (prefix > 1)
	        		System.exit(0);
	        	}
	        	sb.append(str);
	        }
	        
	    } catch (IOException e) {
	    	e.printStackTrace();
	    } finally {
	    	if (in != null) {
	    		try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
	    	}
	    }

	}

	private static void createBookPart(String newDirName, String bookName, String content, String prefix) {
		String fileName = newDirName + File.separatorChar + prefix + "_" + bookName;
		BufferedWriter out = null;
		try {
	        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF8"));
	        out.write(content);
	    } catch (IOException e) {
			e.printStackTrace();
	    } finally {
	    	if (out != null) {
	    		try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
	    	}
	    }

		try {
			System.out.println(Arrays.toString(getBytesFromFile(new File(fileName))));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);
    
        // Get the size of the file
        long length = file.length();
    
        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }
    
        // Create the byte array to hold the data
        byte[] bytes = new byte[(int)length];
    
        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }
    
        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }
    
        // Close the input stream and return bytes
        is.close();
        return bytes;
    }

}
